package com.lwq.project.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lwq.project.service.Role2muneService;
import com.lwq.project.service.User2roleService;
import com.lwq.project.service.UserService;

@Controller
public class IndexController {
	@Autowired
	private UserService userService;
	@Autowired
	private User2roleService user2roleService;
	@Autowired
	private Role2muneService role2muneService;
	
	@RequestMapping("/login.action")
	public String loginView(HttpServletRequest rq,Model model){
		return "login/login";
	}
	
	@RequestMapping("/loginC.action")
	public String loginCheck(HttpServletRequest rq,String adminName,String adminPwd,Model model){
		String uId = userService.getIdByNameAndPwd(adminName,adminPwd);
		if(!StringUtils.isBlank(uId)){
			String rId = user2roleService.getRoleIdByUserId(uId);
			HttpSession session =rq.getSession();
			session.setAttribute("uPrimary", uId);
			session.setAttribute("rPrimary", rId);
			session.setAttribute("uName", adminName);
			return  "redirect:/index.action";
		}else{
			model.addAttribute("error", "用户名或密码错误！");
			return "login/login";
		}
		
	}
	
	@RequestMapping("/index.action")
	public String toIndex1(HttpServletRequest rq,Model model,HttpSession session){
		String rId = (String) session.getAttribute("rPrimary");
		List<Map<String,Object>> list = role2muneService.getMune(rId);
		model.addAttribute("muneList", list);
		return "/main/index";
	}
	
	@RequestMapping("/*")
	public String errorUrl(){
		return "login/login";
	}
	
	@RequestMapping("/cancel.action")
	public String cancel(HttpServletRequest request){
//		HttpSession session = request.getSession();
//		session.invalidate();
		return "login/login";
	}
}
